

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">


<html xmlns="http://www.w3.org/1999/xhtml">
  <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

    <title>saml2.authn_request &mdash; SAML Python library classes and methods</title>

    <link rel="stylesheet" href="../../_static/default.css" type="text/css" />
    <link rel="stylesheet" href="../../_static/pygments.css" type="text/css" />

    <script type="text/javascript">
      var DOCUMENTATION_OPTIONS = {
        URL_ROOT:    '../../',
        VERSION:     '',
        COLLAPSE_INDEX: false,
        FILE_SUFFIX: '.html',
        HAS_SOURCE:  true
      };
    </script>
    <script type="text/javascript" src="../../_static/jquery.js"></script>
    <script type="text/javascript" src="../../_static/underscore.js"></script>
    <script type="text/javascript" src="../../_static/doctools.js"></script>
    <link rel="top" title="SAML Python library classes and methods" href="../../index.html" />
    <link rel="up" title="Class code" href="../index.html" />
  </head>
  <body>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             accesskey="I">index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Class Index"
             >modules</a> |</li>
        <li><a href="../../index.html">SAML Python library classes and methods</a> &raquo;</li>
          <li><a href="../index.html" accesskey="U">Class code</a> &raquo;</li>
      </ul>
    </div>

    <div class="document">
      <div class="documentwrapper">
        <div class="bodywrapper">
          <div class="body">

  <h1>Source code for saml2.authn_request</h1><div class="highlight"><pre>
<span class="c"># -*- coding: utf-8 -*-</span>

<span class="kn">from</span> <span class="nn">base64</span> <span class="kn">import</span> <span class="n">b64encode</span>
<span class="kn">from</span> <span class="nn">datetime</span> <span class="kn">import</span> <span class="n">datetime</span>
<span class="kn">from</span> <span class="nn">zlib</span> <span class="kn">import</span> <span class="n">compress</span>

<span class="kn">from</span> <span class="nn">saml2.utils</span> <span class="kn">import</span> <span class="n">OneLogin_Saml2_Utils</span>
<span class="kn">from</span> <span class="nn">saml2.constants</span> <span class="kn">import</span> <span class="n">OneLogin_Saml2_Constants</span>


<div class="viewcode-block" id="OneLogin_Saml2_Authn_Request"><a class="viewcode-back" href="../../saml2.html#saml2.authn_request.OneLogin_Saml2_Authn_Request">[docs]</a><span class="k">class</span> <span class="nc">OneLogin_Saml2_Authn_Request</span><span class="p">:</span>

    <span class="k">def</span> <span class="nf">__init__</span><span class="p">(</span><span class="bp">self</span><span class="p">,</span> <span class="n">settings</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Constructs the AuthnRequest object.</span>

<span class="sd">        Arguments are:</span>
<span class="sd">            * (OneLogin_Saml2_Settings)   settings. Setting data</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="bp">self</span><span class="o">.</span><span class="n">__settings</span> <span class="o">=</span> <span class="n">settings</span>

        <span class="n">sp_data</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__settings</span><span class="o">.</span><span class="n">get_sp_data</span><span class="p">()</span>
        <span class="n">security</span> <span class="o">=</span> <span class="bp">self</span><span class="o">.</span><span class="n">__settings</span><span class="o">.</span><span class="n">get_security_data</span><span class="p">()</span>

        <span class="n">uid</span> <span class="o">=</span> <span class="n">OneLogin_Saml2_Utils</span><span class="o">.</span><span class="n">generate_unique_id</span><span class="p">()</span>
        <span class="n">issue_instant</span> <span class="o">=</span> <span class="n">OneLogin_Saml2_Utils</span><span class="o">.</span><span class="n">parse_time_to_SAML</span><span class="p">(</span>
            <span class="nb">int</span><span class="p">(</span><span class="n">datetime</span><span class="o">.</span><span class="n">now</span><span class="p">()</span><span class="o">.</span><span class="n">strftime</span><span class="p">(</span><span class="s">&quot;</span><span class="si">%s</span><span class="s">&quot;</span><span class="p">))</span>
        <span class="p">)</span>

        <span class="n">name_id_policy_format</span> <span class="o">=</span> <span class="n">sp_data</span><span class="p">[</span><span class="s">&#39;NameIDFormat&#39;</span><span class="p">]</span>
        <span class="k">if</span> <span class="s">&#39;wantNameIdEncrypted&#39;</span> <span class="ow">in</span> <span class="n">security</span> <span class="ow">and</span> <span class="n">security</span><span class="p">[</span><span class="s">&#39;wantNameIdEncrypted&#39;</span><span class="p">]:</span>
            <span class="n">name_id_policy_format</span> <span class="o">=</span> <span class="n">OneLogin_Saml2_Constants</span><span class="o">.</span><span class="n">NAMEID_ENCRYPTED</span>

        <span class="n">provider_name_str</span> <span class="o">=</span> <span class="s">&#39;&#39;</span>
        <span class="n">organization_data</span> <span class="o">=</span> <span class="n">settings</span><span class="o">.</span><span class="n">get_organization</span><span class="p">()</span>
        <span class="k">if</span> <span class="nb">isinstance</span><span class="p">(</span><span class="n">organization_data</span><span class="p">,</span> <span class="nb">dict</span><span class="p">):</span>
            <span class="n">langs</span> <span class="o">=</span> <span class="n">organization_data</span><span class="o">.</span><span class="n">keys</span><span class="p">()</span>
            <span class="k">if</span> <span class="s">&#39;en-US&#39;</span> <span class="ow">in</span> <span class="n">langs</span><span class="p">:</span>
                <span class="n">lang</span> <span class="o">=</span> <span class="s">&#39;en-US&#39;</span>
            <span class="k">else</span><span class="p">:</span>
                <span class="n">lang</span> <span class="o">=</span> <span class="n">langs</span><span class="p">[</span><span class="mi">0</span><span class="p">]</span>
            <span class="k">if</span> <span class="s">&#39;displayname&#39;</span> <span class="ow">in</span> <span class="n">organization_data</span><span class="p">[</span><span class="n">lang</span><span class="p">]</span> <span class="ow">and</span> <span class="n">organization_data</span><span class="p">[</span><span class="n">lang</span><span class="p">][</span><span class="s">&#39;displayname&#39;</span><span class="p">]</span> <span class="ow">is</span> <span class="ow">not</span> <span class="bp">None</span><span class="p">:</span>
                <span class="n">provider_name_str</span> <span class="o">=</span> <span class="s">&#39;ProviderName=&quot;</span><span class="si">%s</span><span class="s">&quot;&#39;</span> <span class="o">%</span> <span class="n">organization_data</span><span class="p">[</span><span class="n">lang</span><span class="p">][</span><span class="s">&#39;displayname&#39;</span><span class="p">]</span>

        <span class="n">request</span> <span class="o">=</span> <span class="s">&quot;&quot;&quot;&lt;samlp:AuthnRequest</span>
<span class="s">    xmlns:samlp=&quot;urn:oasis:names:tc:SAML:2.0:protocol&quot;</span>
<span class="s">    xmlns:saml=&quot;urn:oasis:names:tc:SAML:2.0:assertion&quot;</span>
<span class="s">    ID=&quot;</span><span class="si">%(id)s</span><span class="s">&quot;</span>
<span class="s">    Version=&quot;2.0&quot;</span>
<span class="s">    </span><span class="si">%(provider_name)s</span><span class="s"></span>
<span class="s">    IssueInstant=&quot;</span><span class="si">%(issue_instant)s</span><span class="s">&quot;</span>
<span class="s">    ProtocolBinding=&quot;urn:oasis:names:tc:SAML:2.0:bindings:HTTP-POST&quot;</span>
<span class="s">    AssertionConsumerServiceURL=&quot;</span><span class="si">%(assertion_url)s</span><span class="s">&quot;&gt;</span>
<span class="s">    &lt;saml:Issuer&gt;</span><span class="si">%(entity_id)s</span><span class="s">&lt;/saml:Issuer&gt;</span>
<span class="s">    &lt;samlp:NameIDPolicy</span>
<span class="s">        Format=&quot;</span><span class="si">%(name_id_policy)s</span><span class="s">&quot;</span>
<span class="s">        AllowCreate=&quot;true&quot; /&gt;</span>
<span class="s">    &lt;samlp:RequestedAuthnContext Comparison=&quot;exact&quot;&gt;</span>
<span class="s">        &lt;saml:AuthnContextMethodRef&gt;urn:oasis:names:tc:SAML:2.0:ac:classes:PasswordProtectedTransport&lt;/saml:AuthnContextMethodRef&gt;</span>
<span class="s">    &lt;/samlp:RequestedAuthnContext&gt;</span>
<span class="s">&lt;/samlp:AuthnRequest&gt;&quot;&quot;&quot;</span> <span class="o">%</span> <span class="p">{</span>
            <span class="s">&#39;id&#39;</span><span class="p">:</span> <span class="n">uid</span><span class="p">,</span>
            <span class="s">&#39;provider_name&#39;</span><span class="p">:</span> <span class="n">provider_name_str</span><span class="p">,</span>
            <span class="s">&#39;issue_instant&#39;</span><span class="p">:</span> <span class="n">issue_instant</span><span class="p">,</span>
            <span class="s">&#39;assertion_url&#39;</span><span class="p">:</span> <span class="n">sp_data</span><span class="p">[</span><span class="s">&#39;assertionConsumerService&#39;</span><span class="p">][</span><span class="s">&#39;url&#39;</span><span class="p">],</span>
            <span class="s">&#39;entity_id&#39;</span><span class="p">:</span> <span class="n">sp_data</span><span class="p">[</span><span class="s">&#39;entityId&#39;</span><span class="p">],</span>
            <span class="s">&#39;name_id_policy&#39;</span><span class="p">:</span> <span class="n">name_id_policy_format</span><span class="p">,</span>
        <span class="p">}</span>

        <span class="bp">self</span><span class="o">.</span><span class="n">__authn_request</span> <span class="o">=</span> <span class="n">request</span>

<div class="viewcode-block" id="OneLogin_Saml2_Authn_Request.get_request"><a class="viewcode-back" href="../../saml2.html#saml2.authn_request.OneLogin_Saml2_Authn_Request.get_request">[docs]</a>    <span class="k">def</span> <span class="nf">get_request</span><span class="p">(</span><span class="bp">self</span><span class="p">):</span>
        <span class="sd">&quot;&quot;&quot;</span>
<span class="sd">        Returns unsigned AuthnRequest.</span>
<span class="sd">        :return: Unsigned AuthnRequest</span>
<span class="sd">        :rtype: str object</span>
<span class="sd">        &quot;&quot;&quot;</span>
        <span class="n">deflated_request</span> <span class="o">=</span> <span class="n">compress</span><span class="p">(</span><span class="bp">self</span><span class="o">.</span><span class="n">__authn_request</span><span class="p">)[</span><span class="mi">2</span><span class="p">:</span><span class="o">-</span><span class="mi">4</span><span class="p">]</span>
        <span class="k">return</span> <span class="n">b64encode</span><span class="p">(</span><span class="n">deflated_request</span><span class="p">)</span></div></div>
</pre></div>

          </div>
        </div>
      </div>
      <div class="sphinxsidebar">
        <div class="sphinxsidebarwrapper">
<div id="searchbox" style="display: none">
  <h3>Quick search</h3>
    <form class="search" action="../../search.html" method="get">
      <input type="text" name="q" />
      <input type="submit" value="Go" />
      <input type="hidden" name="check_keywords" value="yes" />
      <input type="hidden" name="area" value="default" />
    </form>
    <p class="searchtip" style="font-size: 90%">
    Enter search terms or a module, class or function name.
    </p>
</div>
<script type="text/javascript">$('#searchbox').show(0);</script>
        </div>
      </div>
      <div class="clearer"></div>
    </div>
    <div class="related">
      <h3>Navigation</h3>
      <ul>
        <li class="right" style="margin-right: 10px">
          <a href="../../genindex.html" title="General Index"
             >index</a></li>
        <li class="right" >
          <a href="../../py-modindex.html" title="Python Class Index"
             >modules</a> |</li>
        <li><a href="../../index.html">SAML Python library classes and methods</a> &raquo;</li>
          <li><a href="../index.html" >Class code</a> &raquo;</li>
      </ul>
    </div>
    <div class="footer">
      Created using <a href="http://sphinx.pocoo.org/">Sphinx</a> 1.1.3.
    </div>
  </body>
</html>
